package com.twu.biblioteca.dao;

import com.twu.biblioteca.model.Entity;
import com.twu.biblioteca.model.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by minggong on 7/19/14.
 */
public abstract class EntityResource {
    protected abstract void readFile(String file);
    protected List<Entity> entities;
    protected int generateId = 1;

    protected EntityResource() {
        entities = new ArrayList<Entity>();
    }

    public List<Entity> listEntities() {
        return entities;
    }

    public boolean checkoutEntity(int id, User user) {
        for (Entity entity : entities) {
            if (entity.getId() == id && !entity.isChecked()) {
                entity.setChecked(true);
                entity.setBorrowId(user.getId());
                return true;
            }
        }
        return false;
    }

    public List<Entity> getCheckoutList() {
        List<Entity> checkedList = new ArrayList<Entity>();
        for (Entity entity : entities) {
            if (entity.isChecked()) {
                checkedList.add(entity);
            }
        }
        return checkedList;
    }

    public List<Entity> getAvailableList() {
        List<Entity> availableList = new ArrayList<Entity>();
        for (Entity entity : entities) {
            if (!entity.isChecked()) {
                availableList.add(entity);
            }
        }
        return availableList;
    }

    public boolean returnEntity(int id) {
        for (Entity entity : entities) {
            if (entity.getId() == id) {
                entity.setChecked(false);
                entity.setBorrowId(0);
                return true;
            }
        }
        return false;
    }
}
